package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.media.utils.DateUtils;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import zhang.lao.build.kit.LogKit;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.build.tool.date.DateStyle;
import zhang.lao.build.tool.date.DateUtil;
import zhang.lao.dao.base.WnMachineDeviceDao;
import zhang.lao.dao.base.WnMachineDeviceImeiDao;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.dao.base.WnMachineDeviceLinkDao;
import zhang.lao.pojo.WnConstant;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.req.WnMacheineDeviceImeiReq;
import zhang.lao.pojo.console.resp.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import zhang.lao.build.tool.UUIDTool;
import zhang.lao.service.console.SysSequenceFactory;
import zhang.lao.service.search.SearchService;

/**
* 
* @author 
* @create 
**/
 @Service
public class WnMachineDeviceImeiService{
	@Resource
	private WnMachineDeviceImeiDao wnMachineDeviceImeiDao;
	@Resource
    private SysSequenceFactory sysSequenceFactory;
	@Resource
	private WnMachineDeviceDao wnMachineDeviceDao;
	@Resource
	private WnMachineDeviceLinkDao wnMachineDeviceLinkDao;
	@Resource
	private SearchService searchService;
	public String add(){
		return "console/wnMachineDeviceImei/wnMachineDeviceImei_form";
	}

	public String edit(ModelMap modelMap,Integer id){
        WnMachineDevice wnMachineDevice = wnMachineDeviceDao.selectByPrimaryKey(id);
        WnMachineDeviceImeiExample wnMachineDeviceImeiExample = new WnMachineDeviceImeiExample();
        wnMachineDeviceImeiExample.createCriteria().andDeviceIdEqualTo(id);
        WnMacheineDeviceImeiReq wnMacheineDeviceImeiReq = new WnMacheineDeviceImeiReq();
        WnMachineDeviceImei first = ListUtils.getFirst(wnMachineDeviceImeiDao.selectByExample(wnMachineDeviceImeiExample));
        if(first!=null){
            wnMacheineDeviceImeiReq.setBuyTime(DateUtil.DateToString(first.getBuyTime(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            wnMacheineDeviceImeiReq.setImei(first.getImei());
            wnMacheineDeviceImeiReq.setSimcard(first.getSimcard());
            wnMacheineDeviceImeiReq.setValidityPeriod(DateUtil.DateToString(first.getValidityPeriod(), DateStyle.YYYY_MM_DD_HH_MM_SS));
        }


        wnMacheineDeviceImeiReq.setDeviceNumber(wnMachineDevice.getDeviceNumber());
        wnMacheineDeviceImeiReq.setDeviceType(wnMachineDevice.getDeviceType());
        wnMacheineDeviceImeiReq.setFarmId(wnMachineDevice.getFarmId());

        wnMacheineDeviceImeiReq.setId(wnMachineDevice.getId());
        modelMap.put("wnMachineDeviceImei", wnMacheineDeviceImeiReq);
		return "console/wnMachineDeviceImei/wnMachineDeviceImei_form";
	}

	public String list(){
		return "console/wnMachineDeviceImei/wnMachineDeviceImei_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq,WnMacheineDeviceImeiReq wnMacheineDeviceImeiReq,ConsoleContext consoleContext){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
        wnMacheineDeviceImeiReq.setFarms(consoleContext.getFarms());
		List<WnMacheineDeviceImeiResp> wnMacheineDeviceImeiRespList = wnMachineDeviceImeiDao.findWnMacheineDeviceImeiRespList(wnMacheineDeviceImeiReq);
		return new BootStrapGridResp(page.getTotal(),wnMacheineDeviceImeiRespList);
	}

	@Transactional
	public HttpResult save(String formObjectJson,String machineId, ConsoleContext consoleContext){
		WnMacheineDeviceImeiReq wnMacheineDeviceImeiReq= JSON.parseObject(formObjectJson,WnMacheineDeviceImeiReq.class);
			Integer id=wnMacheineDeviceImeiReq.getId();
		if (id!=null) {
			WnMachineDeviceImeiExample wnMachineDeviceImeiExample = new WnMachineDeviceImeiExample();
			wnMachineDeviceImeiExample.createCriteria().andDeviceIdEqualTo(id);
			WnMachineDeviceImei wnMachineDeviceImei1 = ListUtils.getFirst(wnMachineDeviceImeiDao.selectByExample(wnMachineDeviceImeiExample));
			wnMachineDeviceImei1.setBuyTime(DateUtil.StringToDate(wnMacheineDeviceImeiReq.getBuyTime()));
			wnMachineDeviceImei1.setImei(wnMacheineDeviceImeiReq.getImei());
			wnMachineDeviceImei1.setSimcard(wnMacheineDeviceImeiReq.getSimcard());
			wnMachineDeviceImei1.setValidityPeriod(DateUtil.StringToDate(wnMacheineDeviceImeiReq.getValidityPeriod()));
			if(wnMachineDeviceImei1!=null){
				wnMachineDeviceImeiDao.updateByPrimaryKeySelective(wnMachineDeviceImei1);
			}else{
				wnMachineDeviceImei1.setId(UUIDTool.getUUID());
				wnMachineDeviceImei1.setDeviceId(id);
				wnMachineDeviceImeiDao.insertSelective(wnMachineDeviceImei1);
			}
			WnMachineDevice wnMachineDevice = wnMachineDeviceDao.selectByPrimaryKey(id);
			wnMachineDevice.setDeviceNumber(wnMacheineDeviceImeiReq.getDeviceNumber());
            wnMachineDevice.setDeviceType(wnMacheineDeviceImeiReq.getDeviceType());
            wnMachineDevice.setFarmId(wnMacheineDeviceImeiReq.getFarmId());
            wnMachineDeviceDao.updateByPrimaryKey(wnMachineDevice);
		}else{

            WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
            wnMachineDeviceExample.createCriteria().andDeviceNumberEqualTo(wnMacheineDeviceImeiReq.getDeviceNumber());
            if(wnMachineDeviceDao.countByExample(wnMachineDeviceExample)>0){
                return HttpResultUtil.buildError(HttpResultEnum.DEVICE_NUMBER_ERR);
            }
            Integer deviceId = sysSequenceFactory.getSequence("wn_machine_device");
			WnMachineDevice wnMachine = new WnMachineDevice();
            wnMachine.setId(deviceId);
			wnMachine.setCreateTime(new Date());
			wnMachine.setCreateUser(consoleContext.getStrUserId());
			wnMachine.setDeviceNumber(wnMacheineDeviceImeiReq.getDeviceNumber());
			wnMachine.setFarmId(wnMacheineDeviceImeiReq.getFarmId());
			wnMachine.setDeviceType(WnConstant.DEVICE_TYPE_gt06);
			wnMachineDeviceDao.insertSelective(wnMachine);
			WnMachineDeviceImei wnMachineDeviceImei1 = new WnMachineDeviceImei();
			wnMachineDeviceImei1.setBuyTime(DateUtil.StringToDate(wnMacheineDeviceImeiReq.getBuyTime()));
			wnMachineDeviceImei1.setImei(wnMacheineDeviceImeiReq.getImei());
			wnMachineDeviceImei1.setSimcard(wnMacheineDeviceImeiReq.getSimcard());
			wnMachineDeviceImei1.setValidityPeriod(DateUtil.StringToDate(wnMacheineDeviceImeiReq.getValidityPeriod()));
			wnMachineDeviceImei1.setId(UUIDTool.getUUID());
			wnMachineDeviceImei1.setDeviceId(deviceId);
			wnMachineDeviceImeiDao.insertSelective(wnMachineDeviceImei1);
			if(StringUtils.isNotBlank(machineId)){
					WnMachineDeviceLink wnMachineDeviceLink = new WnMachineDeviceLink();
					wnMachineDeviceLink.setDeviceId(deviceId);
					wnMachineDeviceLink.setMachineId(machineId);
					wnMachineDeviceLink.setId(UUIDTool.getUUID());
					wnMachineDeviceLinkDao.insert(wnMachineDeviceLink);
			}
			try {
				searchService.rounting( deviceId);
			} catch (HttpProcessException e) {
				e.printStackTrace();
			}
		}
		return HttpResultUtil.buildSuccess();

	}

	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
			WnMachineDeviceImeiExample wnMachineDeviceImeiExample = new WnMachineDeviceImeiExample();
			wnMachineDeviceImeiExample.createCriteria().andDeviceIdEqualTo(Integer.parseInt(id));
			WnMachineDeviceLinkExample wnMachineDeviceLinkExample =new WnMachineDeviceLinkExample();
			wnMachineDeviceLinkExample.createCriteria().andDeviceIdEqualTo(Integer.parseInt(id));
			wnMachineDeviceDao.deleteByPrimaryKey(Integer.parseInt(id));
			wnMachineDeviceLinkDao.deleteByExample(wnMachineDeviceLinkExample);
			wnMachineDeviceImeiDao.deleteByExample(wnMachineDeviceImeiExample);
		}
		return HttpResultUtil.buildSuccess("删除成功(仅解除与农机关联)!");
	}
}
